package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/admin/dish")
@Slf4j
@RestController
@Api(tags="菜品接口")
public class DishController {


  @Autowired
  private DishService dishService;

  @Autowired
  private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);//后绪步骤开发

      //清除缓存
      redisTemplate.delete("dish_"+dishDTO.getCategoryId());
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(("菜品分页查询"))
    public Result<PageResult> page(DishPageQueryDTO dto){
    log.info("菜品分页查询：{}",dto);
    PageResult pageResult = dishService.pageQuery(dto);
    return Result.success(pageResult);
    }

  /**
   * 删除菜品
   * @param ids
   * @return
   */
  @DeleteMapping
  @ApiOperation("菜品批量删除")
  public Result delete(@RequestParam List<Long> ids){
    log.info("菜品批量删除：{}",ids);
    dishService.deleteBatch(ids);
    int index = ids.size();
    for(int i = 0; i < index; i++){
      Long temp = dishService.getCategoryId(ids.get(i));
      redisTemplate.delete("dish_"+temp);
    }

    return Result.success();
  }

  /**
   * 根据id查询菜品
   * @param id
   * @return
   */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
      log.info("根据id查询菜品：{}",id);
      DishVO dishVO = dishService.getById(id);
      return Result.success(dishVO);
    }

  /**
   * 修改菜品
   * @param dishDTO
   * @return
   */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
    log.info("修改菜品：{}",dishDTO);
    //删除原来的分类
    Long originId = dishDTO.getCategoryId();
    dishService.update(dishDTO);
    //删除现在所属的分类：
    Long ans = dishService.getCategoryId(dishDTO.getId());
    log.info("当前所属分类为：{}",ans);
    redisTemplate.delete("dish_"+originId);
    redisTemplate.delete("dish_"+ans);


    return Result.success();
    }

  /**
   * 修改菜品状态起售或者停售
   * @param status
   * @param id
   * @return
   */

    @ApiOperation("启用禁用菜品")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id){
      Long ans = dishService.getCategoryId(id);
      dishService.updateStatus(status,id);
      redisTemplate.delete("dish_"+ans);
      return Result.success();
    }


  //根据分类id查询菜品
  @GetMapping("/list")
  @ApiOperation("根据分类id查询菜品")
  public Result<List<Dish>> list(Long categoryId){//这里的参数记得要是categoryId
    log.info("根据分类id查询菜品：{}",categoryId);
    List<Dish> list=dishService.getByCategoryId(categoryId);
    return Result.success(list);
  }

}
